package com.example.games.mygame;

import java.util.ArrayList;
import java.util.Vector;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.example.games.core.CScene;
import com.example.games.core.CSprite;
import com.example.games.core.GameHelper;
import com.example.games.core.ResourceManager;
import com.example.games.core.ScreenUtil;

public class MapScene implements CScene {

	private String sceneName;

	private boolean drawLine = false;
	private Vector<CSprite> sprites_attacker = new Vector<CSprite>();
	private Vector<CSprite> sprites_defender = new Vector<CSprite>();

	private Vector<CSprite> sprites = new Vector<CSprite>();

	Bitmap nuBmp, arrowBmp, mapBg;
	int num_w, num_h, mapCellWidth, mapCellHeight, screenWidth, screenHeight;

	float degrees = 0;
	private long lastArrowMills;

	public MapScene(String name) {
		sceneName = name;
		mapBg = ResourceManager.getMapImg("bg");
		mapCellWidth = mapBg.getWidth();
		mapCellHeight = mapBg.getHeight();
		screenWidth = GameHelper.getScreenWidth();
		screenHeight = GameHelper.getScreenHeight();
		num_w = screenWidth / mapCellWidth + 1;
		num_h = screenHeight / mapCellHeight + 1;
		GameHelper.printLog("w:" + screenWidth + ",h:" + screenHeight);

		nuBmp = ResourceManager.getMapImg("nu_normal");
		arrowBmp = ResourceManager.getMapImg("jian_normal");

		Bitmap enemyBmp = ResourceManager.getMapImg("enemy1");
		Enemy enemy = new Enemy(enemyBmp, ScreenUtil.DEFAULT_WIDTH
				- enemyBmp.getWidth(), 0);
		addSprite(enemy);
	}

	private void drawBg(Canvas canvas, Paint paint) {
		// int bgW = num_w * mapCellWidth;
		// int bgH = num_h * mapCellHeight;
		// for (int x = 0; x <= bgW; x += mapCellWidth) {
		// for (int y = 0; y <= bgH; y += mapCellHeight) {
		// canvas.drawBitmap(mapBg, x, y, null);
		// }
		// }
		canvas.save();
		float sx = (float) mapCellWidth / screenWidth;
		float sy = (float) mapCellHeight / screenHeight;
		canvas.scale(sx, sy);
		canvas.drawBitmap(mapBg, 0, 0, paint);
		canvas.restore();

		if (drawLine) {
			paint.reset();
			paint.setColor(Color.WHITE);
			paint.setStyle(Paint.Style.STROKE);

			// 绘制方格 4*8 【90*90】
			int startX = ScreenUtil.getCellX();
			int startY = ScreenUtil.getCellY();
			int stopX = startX + ScreenUtil.getCellWH()
					* ScreenUtil.CELL_NUMS_W;
			int stopY = startY + ScreenUtil.getCellWH()
					* ScreenUtil.CELL_NUMS_H;
			for (int i = 0; i <= ScreenUtil.CELL_NUMS_W; i++) {
				canvas.drawLine(startX + ScreenUtil.getCellWH() * i, startY,
						startX + ScreenUtil.getCellWH() * i, stopY, paint);
			}

			for (int j = 0; j <= ScreenUtil.CELL_NUMS_H; j++) {
				canvas.drawLine(startX, startY + ScreenUtil.getCellWH() * j,
						stopX, startY + ScreenUtil.getCellWH() * j, paint);
			}
		}
	}

	@Override
	public void draw(Canvas canvas, Paint paint) {
		// TODO Auto-generated method stub
		drawBg(canvas, paint);

		// 绘制弩和箭
		canvas.save();
		paint.reset();
		int arrowX = ScreenUtil.getScreenX(40);
		int arrowY = ScreenUtil.getScreenY(258);
		canvas.rotate(degrees, arrowX, arrowY);
		if (degrees != 0) {
			paint.setAlpha(50);
		}
		canvas.drawBitmap(arrowBmp, arrowX, arrowY, paint);
		paint.reset();
		canvas.drawBitmap(nuBmp, 0, ScreenUtil.getScreenY(195), paint);
		canvas.restore();

		for (CSprite sprite : sprites) {
			sprite.drawSelf(canvas, paint);
		}

	}

	private ArrayList<CSprite> badSprites = new ArrayList<CSprite>();

	@Override
	public void logic() {
		// TODO Auto-generated method stub

		for (CSprite sprite : sprites) {
			sprite.logic();
			if (sprite.getX() > GameHelper.getScreenWidth()
					|| sprite.getY() > GameHelper.getScreenHeight()
					|| sprite.getX() < 0 || sprite.getY() < 0) {
				badSprites.add(sprite);
			}
		}
		for (CSprite sprite : badSprites) {
			sprites.remove(sprite);
		}

		badSprites.clear();

		for (CSprite sprite : sprites_attacker) {
			if (sprite.getType() == 1) {
				// arrow
				for (CSprite sprite2 : sprites_defender) {
					if (sprite.isCollidedEnemy(sprite2)) {
						sprite2.lostHp(500);
					}
				}
			}
		}

	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return sceneName;
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void addSprite(CSprite sprite) {
		// TODO Auto-generated method stub
		if (sprite.getType() == 1) {
			sprites_attacker.add(sprite);
		} else {
			sprites_defender.add(sprite);
		}
		sprites.add(sprite);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		float downX = e.getX();
		float downY = e.getY();
		int arrowX = ScreenUtil.getScreenX(40);
		int arrowY = ScreenUtil.getScreenY(258);
		double temp = (double) (downY - arrowY) / (downX - arrowX);
		degrees = (float) Math.toDegrees(Math.atan(temp));
		GameHelper.printLog("degrees:" + degrees);
		if (System.currentTimeMillis() - lastArrowMills > 500) {
			lastArrowMills = System.currentTimeMillis();
			SceneManage.getInstance().getRunningScene()
					.addSprite(new Arrow(temp, downY - temp * downX, degrees));
		}
		return true;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		float downX = e2.getX();
		float downY = e2.getY();
		int arrowX = ScreenUtil.getScreenX(40);
		int arrowY = ScreenUtil.getScreenY(258);
		double temp = (double) (downY - arrowY) / (downX - arrowX);
		degrees = (float) Math.toDegrees(Math.atan(temp));
		GameHelper.printLog("degrees:" + degrees);
		if (System.currentTimeMillis() - lastArrowMills > 500) {
			lastArrowMills = System.currentTimeMillis();
			SceneManage.getInstance().getRunningScene()
					.addSprite(new Arrow(temp, downY - temp * downX, degrees));
		}
		return true;
	}

}
